from textual.app import ComposeResult
from textual.screen import ModalScreen
from textual.binding import Binding
from textual.containers import Vertical, Center
from textual.widgets import Footer,Static,Markdown
from textual.message import Message
from textual import on

from ..widgets import Omnibox

class SearchScreen(ModalScreen):

    DEFAULT_CSS = """
    SearchScreen {
        align: center middle;
    }
    SearchScreen Center {
        width: 100%;
    }

    SearchScreen > Vertical {
        background: $boost;
        width: auto;
        height: auto;
        border: round $primary 50%;
        min-width: 40%;
    }
    """

    BINDINGS = [
        Binding("escape", "dismiss_search", "Dismiss search"),
    ]

    def action_dismiss_search(self) -> None:
        self.dismiss("")

    def __init__(self, title: str = "# 搜索", content: str = ""):
        super().__init__()
        self.title = title
        self.content = content

        if self.content != "":
            self.title = f"{self.title}\n{self.content}"

    def compose(self) -> ComposeResult:
        with Vertical():
            with Center():
                yield Markdown(self.title)
                yield Omnibox()
    def on_mount(self):
        '''nothing'''
        self.query_one(Omnibox).focus()

    @on(Omnibox.OnSubmitCommand)
    def on_omnibox_submitted(self, event: Omnibox.OnSubmitCommand):
        self.dismiss(event.text)
    
    def on_message(self):
        self.notify('!!!')


